﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TeacherHelp.FormList.Admin
{
    public partial class UserInfoShowForm : BanFlashingForm
    {
        private AdminMainForm _parentForm;

        private UserInfoShowForm()
        {
            InitializeComponent();
            UserInfoGridView.DataSource = new BLL.UserManager().GetUserAllList();
        }

        public UserInfoShowForm(AdminMainForm _parentForm) : this()
        {
            this._parentForm = _parentForm;
        }

        private void SerachBtn_Click(object sender, EventArgs e)
        {
            string uerName = SerachTxt.Text.Trim();
            UserInfoGridView.DataSource = new BLL.UserManager().GetUserByUserName(uerName);
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _parentForm.UserEdit();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var selectVaule = this.UserInfoGridView.SelectedRows;
            if (selectVaule.Count == 0)
            {
                MessageBox.Show("请选择行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            var role = selectVaule[0].Cells[nameof(ViewModel.User.roleName)].Value.ToString();
            if (role == "超级管理员")
            {
                MessageBox.Show("超级管理员不允许删除！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }

            var userId = selectVaule[0].Cells[nameof(ViewModel.User.Id)].Value.ToInt();
            var isRemove = MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK;
            if (!isRemove) return;

            BLL.UserManager userManager = new BLL.UserManager();
            string msg = userManager.RemoveUser(userId);

            if (MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) == DialogResult.OK)
            {
                this.UserInfoGridView.DataSource = userManager.GetUserAllList();
            }
        }
    }
}
